<?php

namespace Medicina\BackendBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Medicina\KernelBundle\Entity\TipoDeCargo;
use Medicina\BackendBundle\Form\TipodecargoType;
use Medicina\BackendBundle\Form\TipodecargoSearchType;

/**
 * Tipodecargo controller.
 *
 */
class TipodecargoController extends Controller {

    /**
     * Lists all Tipodecargo entities.
     *
     */
    public function indexAction() {
        $entity = new TipoDeCargo();
        $searchForm = $this->createForm(new TipodecargoSearchType(), $entity);
        $TiposDeCargo = new \Doctrine\Common\Collections\ArrayCollection();
        $paginator = $this->get('ideup.simple_paginator');
        $em = $this->getDoctrine()->getEntityManager();
        $filter = false;
        $paginador = $this->get('ideup.simple_paginator');
        $paginador->setItemsPerPage(10);
        $request = $this->getRequest();
        if ($request->getMethod() === 'POST') {
            $searchForm->bindRequest($request);
            if ($searchForm->isValid()) {
                $nombre = $searchForm->getData()->getNombre();
                $TiposDeCargo = $em->getRepository('MedicinaKernelBundle:Tipodecargo')->getTipoCargoByCriteria($nombre);
                $filter = true;
            } else {
                $TiposDeCargo = $em->getRepository('MedicinaKernelBundle:Tipodecargo')->findAll();
            }
        } else {
            $TiposDeCargo = $em->getRepository('MedicinaKernelBundle:Tipodecargo')->findAll();
        }
        $entities = $paginador->paginate($TiposDeCargo)->getResult();


        return $this->render('BackendBundle:Tipodecargo:index.html.twig', array(
                    'entities' => $entities,
                    'paginador' => $paginador,
                    'searchTipoCargo' => $searchForm->createView(),
                    'filter' => $filter,
                    'delete_form' => $this->createDeleteForm()->createView()
                ));
    }

    /**
     * Finds and displays a Tipodecargo entity.
     *
     */
    public function showAction($id) {
        $em = $this->getDoctrine()->getEntityManager();

        $entity = $em->getRepository('MedicinaKernelBundle:Tipodecargo')->find($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find Tipodecargo entity.');
        }

        $deleteForm = $this->createDeleteForm($id);

        return $this->render('BackendBundle:Tipodecargo:show.html.twig', array(
                    'entity' => $entity,
                    'delete_form' => $deleteForm->createView(),
                ));
    }

    /**
     * Displays a form to create a new Tipodecargo entity.
     *
     */
    public function newAction() {
        $entity = new Tipodecargo();
        $form = $this->createForm(new TipodecargoType(), $entity);

        return $this->render('BackendBundle:Tipodecargo:new.html.twig', array(
                    'entity' => $entity,
                    'form' => $form->createView()
                ));
    }

    /**
     * Creates a new Tipodecargo entity.
     *
     */
    public function createAction() {
        $entity = new Tipodecargo();
        $request = $this->getRequest();
        $form = $this->createForm(new TipodecargoType(), $entity);
        $form->bindRequest($request);

       // if ($form->isValid()) {
            $em = $this->getDoctrine()->getEntityManager();
            try {
                $em->persist($entity);
                $em->flush();
                $this->get('session')->setFlash('notice', 'Se guardo el nuevo Tipo de Cargo correctamente');

                return $this->redirect($this->generateUrl('tipocargo'));

            } catch (\PDOException $e) {
                $this->get('session')->setFlash('error', 'Ya existe un Tipo de cargo con el mismo codigo o nombre');
                //   return $this->redirect($this->generateUrl('tipocargo_new'));
                return $this->render('BackendBundle:Tipodecargo:new.html.twig', array(
                            'entity' => $entity,
                            'form' => $form->createView()
                        ));
            }
   //     } else {
    //        $this->get('session')->setFlash('error', 'Ya existe un cargo con el codigo o nombre');
            //                    return $this->redirect($this->generateUrl('tipocargo_new'));}

     //       return $this->render('BackendBundle:Tipodecargo:new.html.twig', array(
       //                 'entity' => $entity,
        //                'form' => $form->createView()
        //            ));
        }
    

    /**
     * Displays a form to edit an existing Tipodecargo entity.
     *
     */
    public function editAction($id) {
        $em = $this->getDoctrine()->getEntityManager();

        $entity = $em->getRepository('MedicinaKernelBundle:TipoDeCargo')->find($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find Tipodecargo entity.');
        }

        $editForm = $this->createForm(new TipodecargoType(), $entity);
        $deleteForm = $this->createDeleteForm($id);

        return $this->render('BackendBundle:Tipodecargo:edit.html.twig', array(
                    'entity' => $entity,
                    'edit_form' => $editForm->createView(),
                    'delete_form' => $deleteForm->createView(),
                ));
    }

    /**
     * Edits an existing Tipodecargo entity.
     *
     */
    public function updateAction($id) {
        $em = $this->getDoctrine()->getEntityManager();

        $entity = $em->getRepository('MedicinaKernelBundle:TipoDeCargo')->find($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find TipoDeCargo entity.');
        }

        $editForm = $this->createForm(new TipodecargoType(), $entity);
        $deleteForm = $this->createDeleteForm($id);

        $request = $this->getRequest();

        $editForm->bindRequest($request);

     //   if ($editForm->isValid()) {

            try {
                $em->persist($entity);
                $em->flush();
               $this->get('session')->setFlash('notice', 'Se editaron los datos correctamente');

                return $this->redirect($this->generateUrl('tipocargo', array('id' => $id)));


               
            } catch (\PDOException $e) {
                $this->get('session')->setFlash('error', 'Ya existe un tipo de cargo con el mismo código o nombre.');
            
            return $this->render('BackendBundle:Tipodecargo:edit.html.twig', array(
                        'entity' => $entity,
                        'edit_form' => $editForm->createView(),
                        'delete_form' => $deleteForm->createView(),
                    ));}
     //   }
         //               $this->get('session')->setFlash('error', 'Ya existe un tipo de cargo con el mismo código o nombre.');
         //                return $this->render('BackendBundle:Tipodecargo:edit.html.twig', array(
          //                  'entity' => $entity,
          //                  'edit_form' => $editForm->createView(),
          //                  'delete_form' => $deleteForm->createView(),
          //              ));
    }

    /**
     * Deletes a Tipodecargo entity.
     *
     */
    public function deleteAction($id) {
        $em = $this->getDoctrine()->getEntityManager();
        $entity = $em->getRepository('MedicinaKernelBundle:TipoDeCargo')->find($id);

        if (!$entity) {
            $ $this->get('session')->setFlash('error', 'No se encontro el tipo de cargo');
        } else {
            try {
                $distinguible = $entity->getNombre();
                $em->remove($entity);
                $em->flush();
                $this->get('session')->setFlash('notice', 'Tipo de cargo   ' . $distinguible . '  borrado con exito');
            } catch (\PDOException $e) {
                $this->get('session')->setFlash('error', 'No se puede borrar el tipo de cargo ' . $distinguible . ' , esta siendo usado por un cargo o un concurso');
            }
        }
        return $this->redirect($this->generateUrl('tipocargo'));
    }

    private function createDeleteForm() {
        return $this->createFormBuilder()->getForm();
    }

    public function getConcursosAction($id) {
        $httpKernel = $this->container->get('http_kernel');
        $response = $httpKernel->forward(
                'BackendBundle:Concurso:getByTipodecargo', array(
            "tipodecargoId" => $id,
                )
        );
        return $response;
    }

}
